#include "ReBar.h"
#include <assert.h>
#include "FrameWnd.h"

CReBar::CReBar()
{
   m_pFrame = NULL;
}

CReBar::~CReBar()
{
}

HWND CReBar::Create(HWND hParent, RECT& rcPos, UINT uID, DWORD dwCtrlStyle, DWORD dwStyle)
{
   HWND hWnd = ::CreateWindowEx(WS_EX_TOOLWINDOW , REBARCLASSNAME, NULL, dwStyle | dwCtrlStyle, 
                              rcPos.left, rcPos.top, rcPos.right - rcPos.left, rcPos.bottom - rcPos.top,
                              hParent, (HMENU)uID, _Module.GetModuleInstance(), NULL);
   if (!hWnd) {
      return NULL;
   }
   
   Attach(hWnd);
   REBARINFO rbi;
   rbi.cbSize = sizeof (rbi);
   rbi.fMask = 0;
   rbi.himl = (HIMAGELIST)NULL;
   if (!SendMessage(m_hWnd, RB_SETBARINFO, 0, (LPARAM)&rbi)) {
      m_hWnd = NULL;
   }
   BOOL bUnicode;
#ifdef UNICODE 
   bUnicode = TRUE;
#else
   bUnicode = FALSE;
#endif   
   SendMessage(m_hWnd, RB_SETUNICODEFORMAT, (WPARAM)bUnicode, 0);
   SendMessage(m_hWnd, RB_SETTEXTCOLOR, 0, (LPARAM)RGB(0,0,0));
   return m_hWnd;
}

BOOL CReBar::_AddBar (HWND hBar, REBARBANDINFO* pRBBI)
{
   assert (::IsWindow(m_hWnd));
   assert(::IsWindow(hBar));

	pRBBI->cbSize = sizeof(REBARBANDINFO);
	pRBBI->fMask |= RBBIM_CHILD | RBBIM_CHILDSIZE;
	pRBBI->hwndChild = hBar;

   RECT rc;
   ::GetWindowRect(hBar, &rc);

	//WINBUG: COMCTL32.DLL is off by 4 pixels in its sizing logic.  Whatever
	//  is specified as the minimum size, the system rebar will allow that band
	//  to be 4 actual pixels smaller!  That's why we add 4 to the size here.
	pRBBI->cxMinChild = rc.right-rc.left + 4;
	pRBBI->cyMinChild = rc.bottom - rc.top;
   BOOL ret = SendMessage(m_hWnd, RB_INSERTBAND, (WPARAM)-1, (LPARAM)pRBBI);
   if (m_pFrame) {
      m_pFrame->CalcLayout();
   }
   return ret;
}

BOOL CReBar::AddBar(HWND hBar, LPCTSTR pszText, HBITMAP pbmp, DWORD dwStyle)
{
	REBARBANDINFO rbBand;
	rbBand.fMask = RBBIM_STYLE;
	rbBand.fStyle = dwStyle;
   if (pszText) {
		rbBand.fMask |= RBBIM_TEXT;
		rbBand.lpText = const_cast<LPTSTR>(pszText);
	}
	if (pbmp) {
		rbBand.fMask |= RBBIM_BACKGROUND;
		rbBand.hbmBack = pbmp;
	}
	return _AddBar(hBar, &rbBand);
}

BOOL CReBar::AddBar(HWND hBar, COLORREF clrFore, COLORREF clrBack, LPCTSTR pszText, DWORD dwStyle)
{
	REBARBANDINFO rbBand;
	rbBand.fMask = RBBIM_STYLE | RBBIM_COLORS;
	rbBand.fStyle = dwStyle;
	rbBand.clrFore = clrFore;
	rbBand.clrBack = clrBack;
	if (pszText) {
		rbBand.fMask |= RBBIM_TEXT;
		rbBand.lpText = const_cast<LPTSTR>(pszText);
	}
	return _AddBar(hBar, &rbBand);
}

void CReBar::SetFrame(CFrameWnd* pFrame)
{
   m_pFrame = pFrame;
}

